package com.zzyl.serve.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzyl.common.utils.bean.BeanUtils;
import com.zzyl.serve.domain.NursingProjectPlan;
import com.zzyl.serve.dto.NursingPlanDto;
import com.zzyl.serve.service.INursingProjectPlanService;
import com.zzyl.serve.vo.NursingPlanVo;
import com.zzyl.serve.vo.NursingProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.service.INursingPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import java.util.Arrays;
import java.util.stream.Collectors;


/**
 * 护理计划Service业务层处理
 *
 * @author ruoyi
 * @date 2024-10-04
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private INursingProjectPlanService iNursingProjectPlanService;

    @Autowired
    private RedisTemplate redisTemplate;
    private static final String CACHE_KEY_PREFIX = "nursingPlan:all";


    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id) {
        NursingPlan nursingPlan = this.getById(id);
        NursingPlanVo nursingPlanVo = new NursingPlanVo();
        BeanUtils.copyProperties(nursingPlan, nursingPlanVo);
        //批量查询护理项目
        List<NursingProjectPlan> projectPlans = iNursingProjectPlanService.list(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, id));
        List<NursingProjectPlanVo> nursingPlanVos = projectPlans.stream().map(projectPlan -> {
            NursingProjectPlanVo nursingProjectPlanVo = new NursingProjectPlanVo();
            BeanUtils.copyProperties(projectPlan, nursingProjectPlanVo);
            nursingProjectPlanVo.setProjectId(projectPlan.getProjectId().toString());
            nursingProjectPlanVo.setExecuteCycle(projectPlan.getExecuteCycle().toString());
            nursingProjectPlanVo.setExecuteFrequency(Long.valueOf(projectPlan.getExecuteFrequency() + ""));
            nursingProjectPlanVo.setExecuteTime(projectPlan.getExecuteTime());
            nursingProjectPlanVo.setId(projectPlan.getId());//Long.valueOf(projectPlan.getId())
            nursingProjectPlanVo.setPlanId(projectPlan.getPlanId());
            return nursingProjectPlanVo;
        }).collect(Collectors.toList());
        nursingPlanVo.setProjectPlans(nursingPlanVos);
        return nursingPlanVo;
    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan) {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     *
     * @param dto 护理计划
     * @return 结果
     */
    @Override
    @Transactional
    public int insertNursingPlan(NursingPlanDto dto) {
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(dto, nursingPlan);
        this.save(nursingPlan);
        //批量新增服务项目
        List<NursingProjectPlan> projectPlans = dto.getProjectPlans();
        if (!CollectionUtils.isEmpty(projectPlans)) {
            projectPlans.forEach(projectPlan -> projectPlan.setPlanId(nursingPlan.getId()));
        }
        boolean flag = iNursingProjectPlanService.saveBatch(projectPlans);
        //删除缓存
        redisTemplate.delete(CACHE_KEY_PREFIX);
        return flag ? 1 : 0;
    }

    /**
     * 修改护理计划
     *
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Override
    public int updateNursingPlan(NursingPlanDto nursingPlanDto) {
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        if (!CollectionUtils.isEmpty(projectPlans)) {
            //删除原来的护理项目
            iNursingProjectPlanService.remove(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, nursingPlanDto.getId()));
            //重新添加所有的护理项目
            projectPlans.forEach(projectPlan -> {
                projectPlan.setId(nursingPlanDto.getId());
            });
            iNursingProjectPlanService.saveBatch(projectPlans);
        }
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto, nursingPlan);
        boolean flag = updateById(nursingPlan);
        //删除缓存
        redisTemplate.delete(CACHE_KEY_PREFIX);
        return flag ? 1 : 0;
    }

    /**
     * 批量删除护理计划
     *
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanByIds(Long[] ids) {
        //删除缓存
        redisTemplate.delete(CACHE_KEY_PREFIX);
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除护理计划信息
     *
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlanById(Long id) {
        //删除缓存
        redisTemplate.delete(CACHE_KEY_PREFIX);
        return removeById(id) ? 1 : 0;
    }
}
